//
//  ViewController.m
//  NSOperation
//
//  Created by 杨康 on 2017/12/8.
//  Copyright © 2017年 杨康. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //两个需要star操作开启
    NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{
        
    }];
    NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(respondsToOperation:) object:nil];
    //它还有一系列的方法表示状态：executing、finished、cancelled，以及开启和取消：start、cancel，和NSThread一样有main方法可重写。
    [self Action];
}
-(void)Action
{
    NSLog(@"主线程开始");
    
    NSBlockOperation *blockOperation =[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"任务0 %@",[NSThread currentThread]);
        
    }];
    //异步
    for (int i=1; i<5; i++) {
        [blockOperation addExecutionBlock:^{
            NSLog(@"任务%d %@",i,[NSThread currentThread]);
            [NSThread sleepForTimeInterval:3.0];
        }];
    }
    //阻塞操作 单独需要star开启 放入NSOperationQueue会自动开启
    [blockOperation start];
    
    
    NSLog(@"主线程结束");
    
}
-(void)Action2
{
    
    //并行队列+异步任务
    NSLog(@"主线程开始");
    
    NSOperationQueue *queue =[NSOperationQueue new];
    NSBlockOperation *blockOperation =[NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"任务0 %@",[NSThread currentThread]);
        
    }];
    for (int i=1; i<5; i++) {
        [blockOperation addExecutionBlock:^{
            NSLog(@"任务%d %@",i,[NSThread currentThread]);
            [NSThread sleepForTimeInterval:3.0];
        }];
    }
    [queue addOperation:blockOperation];
    //这两个操作相当于阻塞操作
//    [queue waitUntilAllOperationsAreFinished];
//    [blockOperation waitUntilFinished];
    //最大并发数，通过设置这个我们可以有效的控制并发任务的数量，从而对性能进行有效的控制。
//    maxConcurrentOperationCount
    
    NSLog(@"主线程结束");
    
}

-(void)Action3
{
    NSLog(@"主线程开始");
    
    NSOperationQueue *queue=[NSOperationQueue new];

    NSBlockOperation *operation0 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"任务1: %@", [NSThread currentThread]);
        [NSThread sleepForTimeInterval:1.0];
    }];
    
    NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"任务2: %@", [NSThread currentThread]);
        [NSThread sleepForTimeInterval:1.0];
    }];
    NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"任务3: %@", [NSThread currentThread]);
        [NSThread sleepForTimeInterval:1.0];
    }];
    NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
        NSLog(@"任务4: %@", [NSThread currentThread]);
        [NSThread sleepForTimeInterval:1.0];
    }];
    
    [operation1 addDependency:operation0];
    [operation2 addDependency:operation1];
    [operation3 addDependency:operation2];
    
    //不要添加相互依赖。理论上不管任务在任何队列都可以添加依赖，不过不建议这么做。
    
    [queue addOperations:@[operation0,operation1,operation2,operation3] waitUntilFinished:NO];
    
    NSLog(@"主线程结束");
    
}


@end
